﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Reports_ServiceInvoice_InvoiceViews_TDSManagement : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    AccountSettings acntstng = new AccountSettings();
    DataTable dtMain = new DataTable();
    string finyear = "";

    Double totalTDSAmount = 0;
    Double totalBillAmount = 0;
    Double totalTDSAmountN = 0;
    Double totalBillAmountN = 0;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            finyear = Session["finyear"].ToString();
            hidfinyear.Value = Session["finyear"].ToString();
            txtFromDate.Text = Convert.ToString("01/04/" + finyear);
            txtToDate.Text = "31/03/" + (Convert.ToInt32(finyear) + 1).ToString();
            BindGrid(txtFromDate.Text, txtToDate.Text);
            lblCompName.Text = Session["compname"].ToString();
            lblCompName2.Text = Session["compname"].ToString();
        }
    }
    protected void BindGrid(string from, string to)
    {
        if (from != string.Empty && to != string.Empty)
        {
            SqlParameter[] sqlparam = {
                                      new SqlParameter("@Finyear",hidfinyear.Value.ToString()),
                                      new SqlParameter("@FROMDATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(from))),
                                      new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(to))),
                                  };

            dtMain = DU.getdatausingprocedure("USP_IVC_GetBillTDSDetails", sqlparam);
        }
        lblMsg.Text = "TDS Management Register : From " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(from)) + " To " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(to)) + ".";
        lblMsg2.Text = "TDS Management Register : From " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(from)) + " To " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(to)) + ".";
        grdTDSManagementView.DataSource = dtMain;
        grdTDSManagementView.DataBind();
        grdPrint.DataSource = dtMain;
        grdPrint.DataBind();
    }
   
    protected void grdTDSManagementView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Double TDSAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TDS Amount"));
            Double BillAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Total Bill Amount"));

            totalTDSAmount = totalTDSAmount + Math.Abs(TDSAmount);
            totalBillAmount = totalBillAmount + Math.Abs(BillAmount);

            if (Math.Abs(TDSAmount) > 0)
            {
                e.Row.Cells[3].Text = acntstng.GetCurrency(Math.Abs(TDSAmount));
            }
            else
            {
                e.Row.Cells[3].Text = string.Empty;
            }
            if (Math.Abs(BillAmount) > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(BillAmount));
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }

            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[2].Text = "Total";
            e.Row.Cells[3].Text = acntstng.GetCurrency(totalTDSAmount).ToString();
            e.Row.Cells[4].Text = acntstng.GetCurrency(totalBillAmount).ToString();
            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.VerticalAlign = VerticalAlign.Bottom;
        }

    }
    protected void btnCangeDate_Click(object sender, EventArgs e)
    {
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }

    protected void grdTDSManagementView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdTDSManagementView.PageIndex = e.NewPageIndex;
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }
    protected void grdPrint_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Double TDSAmountN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TDS Amount"));
            Double BillAmountN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Total Bill Amount"));

            totalTDSAmountN = totalTDSAmountN + Math.Abs(TDSAmountN);
            totalBillAmountN = totalBillAmountN + Math.Abs(BillAmountN);

            if (Math.Abs(TDSAmountN) > 0)
            {
                e.Row.Cells[3].Text = acntstng.GetCurrency(Math.Abs(TDSAmountN));
            }
            else
            {
                e.Row.Cells[3].Text = string.Empty;
            }
            if (Math.Abs(BillAmountN) > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(BillAmountN));
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }

            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[2].Text = "Total";
            e.Row.Cells[3].Text = acntstng.GetCurrency(totalTDSAmountN).ToString();
            e.Row.Cells[4].Text = acntstng.GetCurrency(totalBillAmountN).ToString();
            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.VerticalAlign = VerticalAlign.Bottom;
        }

    }
 
}
